package com.ef.web.action;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.domain.Leads;
import com.ef.domain.LeadsClassEntity;
import com.ef.domain.LeadsEventHistory;
import com.ef.service.LeadsClassEntityService;
import com.ef.service.LeadsEventHistoryService;
import com.ef.service.LeadsService;
import com.ef.util.CalendarUtil;
import com.ef.util.CustomExample;

public class TouchHistoryAction extends BaseAction<LeadsEventHistory> {

	private static final long serialVersionUID = 1L;

	private LeadsEventHistoryService leadsEventHistoryService;

	private Date selLastTouchStartTime;

	private Date selLastTouchEndTime = new Date();

	private String selName;

	private String selCard;

	private String selContent;

	private List<LeadsEventHistory> calls;

	private LeadsService leadsService;

	public List<LeadsEventHistory> getCalls() {
		return calls;
	}

	public void setCalls(List<LeadsEventHistory> calls) {
		this.calls = calls;
	}

	public LeadsService getLeadsService() {
		return leadsService;
	}

	public void setLeadsService(LeadsService leadsService) {
		this.leadsService = leadsService;
	}

	public List<LeadsClassEntity> getLeadsClassEntityList() {
		return leadsClassEntityList;
	}

	public void setLeadsClassEntityList(
			List<LeadsClassEntity> leadsClassEntityList) {
		this.leadsClassEntityList = leadsClassEntityList;
	}

	public LeadsClassEntityService getLeadsClassEntityService() {
		return leadsClassEntityService;
	}

	public void setLeadsClassEntityService(
			LeadsClassEntityService leadsClassEntityService) {
		this.leadsClassEntityService = leadsClassEntityService;
	}

	private List<LeadsClassEntity> leadsClassEntityList;

	private LeadsClassEntityService leadsClassEntityService;

	public Date getSelLastTouchStartTime() {
		return selLastTouchStartTime;
	}

	public void setSelLastTouchStartTime(Date selLastTouchStartTime) {
		this.selLastTouchStartTime = selLastTouchStartTime;
	}

	public Date getSelLastTouchEndTime() {
		return selLastTouchEndTime;
	}

	public void setSelLastTouchEndTime(Date selLastTouchEndTime) {
		this.selLastTouchEndTime = selLastTouchEndTime;
	}

	public String getSelName() {
		return selName;
	}

	public void setSelName(String selName) {
		this.selName = selName;
	}

	public String getSelCard() {
		return selCard;
	}

	public void setSelCard(String selCard) {
		this.selCard = selCard;
	}

	public String getSelContent() {
		return selContent;
	}

	public void setSelContent(String selContent) {
		this.selContent = selContent;
	}

	public TouchHistoryAction() {

	}

	@SuppressWarnings("serial")
	public String list() {
		this.listResult = leadsEventHistoryService.findByExample(
				new CustomExample<LeadsEventHistory>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						criteria.add(Restrictions.isNotNull("leads"));
						criteria.createAlias("leads", "leads");
						if (selName != null && selName.trim().length() > 0) {
							criteria.add(Restrictions.like("leads.name",
									selName.trim(), MatchMode.ANYWHERE));
						}
						if (selCard != null && selCard.trim().length() > 0) {
							criteria.add(Restrictions.like("leads.eliteCard",
									selCard.trim(), MatchMode.ANYWHERE));
						}
						if (selContent != null
								&& selContent.trim().length() > 0) {
							criteria.add(Restrictions.like("comments",
									selContent.trim(), MatchMode.ANYWHERE));
						}
						if (selLastTouchStartTime != null
								&& selLastTouchEndTime != null) {
							criteria
									.add(Restrictions
											.ge(
													"dealTime",
													CalendarUtil
															.getBeginOfDay(selLastTouchStartTime)));
							criteria.add(Restrictions.le("dealTime",
									CalendarUtil
											.getEndOfDay(selLastTouchEndTime)));
						}
						criteria.addOrder(Order.desc("dealTime"));
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	};

	private String leadsId;

	public String getLeadsId() {
		return leadsId;
	}

	public void setLeadsId(String leadsId) {
		this.leadsId = leadsId;
	}

	private  Leads selleads; 

	public String view() {
		if (leadsId!= null && leadsId.trim().length() > 0) {
			selleads = leadsService.findById(Long.valueOf(leadsId.trim()));
			calls = leadsEventHistoryService.findByLeads(selleads);
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(selleads, 20);
			return EDIT;
		} else {
			return LIST;
		}
	}

 

	public Leads getSelleads() {
		return selleads;
	}

	public void setSelleads(Leads selleads) {
		this.selleads = selleads;
	}

	public LeadsEventHistoryService getLeadsEventHistoryService() {
		return leadsEventHistoryService;
	}

	public void setLeadsEventHistoryService(
			LeadsEventHistoryService leadsEventHistoryService) {
		this.leadsEventHistoryService = leadsEventHistoryService;
	}
}
